// pages/address/index.js
import config from '../../etc/config'
const { $Toast } = require('../../components/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isManage: false,
    typeId: '',
    type: '',
    isShow: false,
    disabled: false
  },

  // 地址管理
  manage() {
    this.setData({
      isManage: !this.data.isManage,
      isShow: false
    })
    if (this.data.addressList && this.data.isManage) {
      for (let i = 0; i < this.data.addressList.length; i++) {
        if (this.data.addressList[i].ischecked) {
          this.setData({
            isShow: true
          })
          return
        } else {
          this.setData({
            isShow: false
          })
        }
      }
    }
  },

  // 改变地址选中状态
  ischeck(e) {
    var idx = e.target.dataset.index
    if (this.data.addressList[idx].ischecked == undefined) {
      this.data.addressList[idx].ischecked = false
    }
    var val = !this.data.addressList[idx].ischecked
    var index = "addressList[" + idx + "].ischecked"
    this.setData({
      [index]: val
    })
    for (let i = 0; i < this.data.addressList.length; i++) {
      if (this.data.addressList[i].ischecked) {
        this.setData({
          isShow: true
        })
        return
      } else {
        this.setData({
          isShow: false
        })
      }
    }
  },

  // 添加地址
  addAddress() {
    wx.navigateTo({
      url: '../add-address/index',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  // 修改地址
  editAddess(e) {
    wx.navigateTo({
      url: '../add-address/index?id=' + e.currentTarget.dataset.id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  // 删除地址
  delAddress() {
    this.setData({
      disabled: true
    })
    var that = this
    var delId = []
    for (let i = 0; i < this.data.addressList.length; i++) {
      if (this.data.addressList[i].ischecked == true) {
        delId.push(this.data.addressList[i].id)
      }
    }
    let token = wx.getStorageSync('token')
    let params = {
      ids: delId
    }
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success(res) {
        if (res.confirm) {
          config._post('/useraddress/deleteUserAddress', token, params).then(res => {
            that.setData({
              disabled: false
            })
            wx.hideLoading();
            if (res.code == 0) {
              $Toast({
                content: '删除成功'
              })
              that.getAddressList()
              that.setData({
                isManage: false,
                isShow: false
              })
            } else {
              $Toast({
                content: res.msg
              })
            }
            wx.hideLoading();
          })
        } else if (res.cancel) {}
      }
    })
  },

  // 获取所有地址
  getAddressList: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      success: function() {
        let token = wx.getStorageSync('token')
        let params = {
          userId: wx.getStorageSync('userId')
        }
        config._post('/useraddress/showUserAddress', token, params).then(res => {
          wx.hideLoading()
          if (res.data) {
            that.setData({
              addressList: res.data
            })
          }
          wx.hideLoading();
        })
      }
    })
  },
  // 选择地址
  selectAddress(e) {
    if (this.data.typeId) {
      let addId = e.currentTarget.dataset.id
      var pages = getCurrentPages(); // 获取页面栈
      var prevPage = pages[pages.length - 2]; // 上一个页面
      prevPage.setData({
        addId: addId
      })
      wx.navigateBack()
    }
  },

  setDefalut(e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      success: function () {
        let token = wx.getStorageSync('token')
        let params = {
          id: e.currentTarget.dataset.id
        }
        config._post('/useraddress/setDefAddress', token, params).then(res => {
          wx.hideLoading()
          wx.hideLoading();
          that.getAddressList()
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var obj = wx.getLaunchOptionsSync()
    // var typeId = obj.query.typeId
    // var type = obj.query.type
    // if (typeId && type) {
    //   this.data.typeId = typeId
    //   this.data.type = type
    //   wx.setNavigationBarTitle({
    //     title: '选择地址'
    //   })
    // } else {
    //   if (options.typeId) {
    //     this.data.typeId = options.typeId
    //     wx.setNavigationBarTitle({
    //       title: '选择地址'
    //     })
    //   } else {
    //     wx.setNavigationBarTitle({
    //       title: '服务地址'
    //     })
    //   }
    // }
    if (options.typeId) {
      this.data.typeId = options.typeId
      wx.setNavigationBarTitle({
        title: '选择地址'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '服务地址'
      })
    }
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})